﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using QLVatTu.Class;

namespace QLVatTu.DanhMuc
{
    public partial class frmDonViTinhVatTu : Form
    {
        public frmDonViTinhVatTu()
        {
            InitializeComponent();
        }

        KetNoiDB cn = new KetNoiDB();
        string tag = "";

        private void load_button(string tag)
        {
            if (tag == "them")
            {
                btnThem.Enabled = false;
                btnSua.Enabled = false;
                btnXoa.Enabled = false;

                btnLuu.Enabled = true;
                btnHuy.Enabled = true;

                cbbNhomVatTu.Enabled = true;
                cbbVatTu.Enabled = true;
                cbbDonViTinh.Enabled = true;
                txtTyLeQuyDoi.Text = "";
                txtTyLeQuyDoi.Enabled = true;


            }
            else if (tag == "sua")
            {
                btnThem.Enabled = false;
                btnSua.Enabled = false;
                btnXoa.Enabled = false;

                btnLuu.Enabled = true;
                btnHuy.Enabled = true;

                cbbNhomVatTu.Enabled = false;
                cbbVatTu.Enabled = false;
                cbbDonViTinh.Enabled = false;
                txtTyLeQuyDoi.Enabled = true;

            }
            else
            {
                btnThem.Enabled = true;
                btnSua.Enabled = true;
                btnXoa.Enabled = true;

                btnLuu.Enabled = false;
                btnHuy.Enabled = false;

                cbbNhomVatTu.Enabled = false;
                cbbVatTu.Enabled = false;
                cbbDonViTinh.Enabled = false;
                txtTyLeQuyDoi.Enabled = false;
            }
        }

        private void loadData()
        {
            cbbNhomVatTu.DataSource = cn.getTable("select * from tbl_NhomVatTu");
            cbbNhomVatTu.DisplayMember = "TenNhomVatTu";
            cbbNhomVatTu.ValueMember = "MaNhomVatTu";

            cbbDonViTinh.DataSource = cn.getTable("select * from tbl_DonViTinh");
            cbbDonViTinh.DisplayMember = "TenDonViTinh";
            cbbDonViTinh.ValueMember = "MaDonViTinh";

            cbbDonViTinhGoc.DataSource = cn.getTable("select * from tbl_DonViTinh");
            cbbDonViTinhGoc.DisplayMember = "TenDonViTinh";
            cbbDonViTinhGoc.ValueMember = "MaDonViTinh";

            colMaVatTu.DataSource = cn.getTable("select * from tbl_VatTu");
            colMaVatTu.DisplayMember = "TenVatTu";
            colMaVatTu.ValueMember = "MaVatTu";

            colMaDonViTinh.DataSource = cn.getTable("select * from tbl_DonViTinh");
            colMaDonViTinh.DisplayMember = "TenDonViTinh";
            colMaDonViTinh.ValueMember = "MaDonViTinh";

            colMaDonViTinhGoc.DataSource = cn.getTable("select * from tbl_DonViTinh");
            colMaDonViTinhGoc.DisplayMember = "TenDonViTinh";
            colMaDonViTinhGoc.ValueMember = "MaDonViTinh";

            load_dgvVatTuDonViTinh();
        }

        private void load_dgvVatTuDonViTinh()
        {
            if (cn.KetNoi())
            {
                DataTable tbl = cn.getTable("SELECT row_number() over(order by tbl_VatTuDonViTinh.MaVatTu) as STT, * from tbl_VatTuDonViTinh");
                dgvVatTuDonViTinh.DataSource = tbl;
            }
        }

        private void frmDonViTinhVatTu_Load(object sender, EventArgs e)
        {
            load_button(tag);
            if (cn.KetNoi())
            {
                loadData();
            }
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            if (dgvVatTuDonViTinh.RowCount == 0) return;
            DialogResult msg = MessageBox.Show("Bạn có chắc chắn muốn xóa?","",MessageBoxButtons.OK,MessageBoxIcon.Error);
            if (msg == DialogResult.Yes)
            {
                cn.Delete_Table("tbl_DonViTinhVatTu","MaVatTu='"+dgvVatTuDonViTinh.CurrentRow.Cells["colMaVatTu"].Value.ToString()+"',MaDonViTinh='"+dgvVatTuDonViTinh.CurrentRow.Cells["colMaDonViTinh"].Value.ToString()+"'");
                MessageBox.Show("Xóa thành công.");
            }
            load_dgvVatTuDonViTinh();
        }

        private void cbbVatTu_SelectedIndexChanged(object sender, EventArgs e)
        {
            
        }

        private void cbbNhomVatTu_SelectedIndexChanged(object sender, EventArgs e)
        {
        }

        private void cbbNhomVatTu_SelectedValueChanged(object sender, EventArgs e)
        {
            if (cbbNhomVatTu.SelectedIndex >= -1)
            {
                cbbVatTu.DataSource = cn.getTable("select * from tbl_VatTu where MaNhomVatTu='" + cbbNhomVatTu.SelectedValue.ToString() + "'");
                cbbVatTu.DisplayMember = "TenVatTu";
                cbbVatTu.ValueMember = "MaVatTu";
            }
            else
            {
                cbbVatTu.DataSource = null;
                cbbVatTu.DisplayMember = "";
            }
        }

        private void dgvVatTuDonViTinh_SelectionChanged(object sender, EventArgs e)
        {
            if (dgvVatTuDonViTinh.SelectedRows.Count > 0)
            {
                cbbNhomVatTu.SelectedValue = cn.LayDuLieu("select MaNhomVatTu from tbl_VatTu where MaVatTu='"+dgvVatTuDonViTinh.CurrentRow.Cells["colMaVatTu"].Value.ToString()+"'");
                cbbVatTu.SelectedValue=dgvVatTuDonViTinh.CurrentRow.Cells["colMaVatTu"].Value.ToString();
                cbbDonViTinh.SelectedValue=dgvVatTuDonViTinh.CurrentRow.Cells["colMaDonViTinh"].Value.ToString();
                cbbDonViTinhGoc.SelectedValue=dgvVatTuDonViTinh.CurrentRow.Cells["colMaDonViTinhGoc"].Value.ToString();
                txtTyLeQuyDoi.Text=dgvVatTuDonViTinh.CurrentRow.Cells["colTyLeQuyDoi"].Value.ToString();
            }
        }

        private void btnThem_Click(object sender, EventArgs e)
        {
            tag = "them";
            load_button(tag);
        }

        private void btnSua_Click(object sender, EventArgs e)
        {
            tag = "sua";
            load_button(tag);
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            if (TopLevel == false) this.Parent.Parent.Controls.Remove(this.Parent);
            else this.Close();
        }

        private void btnLuu_Click(object sender, EventArgs e)
        {

            if (txtTyLeQuyDoi.Text == "")
            {
                MessageBox.Show("Bạn chưa nhập tỷ lệ quy đổi.", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
                txtTyLeQuyDoi.Focus();
                return;
            }
            //string MaDonViTinh = cn.LayDuLieu("select MaDonViTinh from tbl_VatTuDonViTinh where MaVatTu='" + cbbVatTu.SelectedValue.ToString() + "'");
            //if (MaDonViTinh != "")
            //{
            //    MessageBox.Show("Vật tư đã tồn tại đơn vị tính.", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
            //    cbbDonViTinh.Focus();
            //    return;
            //}
            if (tag == "them")
            {
                cn.Insert_Table("tbl_VatTuDonViTinh", "MaVatTu,MaDonViTinh,MaDonViTinhGoc,TyLeQuyDoi", "'" + cbbVatTu.SelectedValue.ToString() + "','" + cbbDonViTinh.SelectedValue.ToString() + "','" + cbbDonViTinhGoc.SelectedValue.ToString() + "','" + txtTyLeQuyDoi.Text + "'");

            }
            else
            {
                cn.Update_Table("tbl_VatTuDonViTinh", "TyLeQuyDoi='" + txtTyLeQuyDoi.Text + "'", "MaVatTu='" + cbbVatTu.SelectedValue.ToString() + "' and MaDonViTinhGoc='" + cbbDonViTinhGoc.SelectedValue.ToString() + "' and MaDonViTinh='" + cbbDonViTinh.SelectedValue.ToString() + "'");
            }
            load_button("");
            load_dgvVatTuDonViTinh();
        }

        private void cbbVatTu_SelectedValueChanged(object sender, EventArgs e)
        {
            if (cbbVatTu.SelectedIndex >= 0)
            {
                cbbDonViTinhGoc.SelectedValue = cn.LayDuLieu("select MaDonViTinhGoc from tbl_VatTu where MaVatTu='"+cbbVatTu.SelectedValue.ToString()+"'");
            }
        }

        private void btnHuy_Click(object sender, EventArgs e)
        {
            load_button("");
        }
    }
}
